Plotly Express in Python¶
来源:https://mp.weixin.qq.com/s/dg390bbxA_LEoggWg6dGlQ
Plotly Express 是一个新的高级 Python 可视化库:它是 Plotly.py 的高级封装,它为复杂的图表提供了一个简单的语法。
受 Seaborn 和 ggplot2 的启发,它专门设计为具有简洁,一致且易于学习的 API :只需一次导入,您就可以在一个函数调用中创建丰富的交互式绘图,包括分面绘图(faceting)、地图、动画和趋势线。它带有数据集、颜色面板和主题,就像 Plotly.py 一样。Plotly是新一代的可视化神器,由TopQ量化团队开源。虽然Ploltly功能非常之强大,但是一直没有得到重视,主要原因还是其设置过于繁琐。因此,Plotly推出了其简化接口:Plotly_express,下文中统一简称为px。
Plotly Express 完全免费:凭借其宽松的开源 MIT 许可证,您可以随意使用它(是的,甚至在商业产品中!)。
最重要的是,Plotly Express 与 Plotly 生态系统的其他部分完全兼容:在您的 Dash 应用程序中使用它,使用 Orca 将您的数据导出为几乎任何文件格式,或使用JupyterLab 图表编辑器在 GUI 中编辑它们!
https://plotly.com/python/plotly-express/
pip install plotly_express
Collecting plotly_express
Downloading plotly_express-0.4.1-py2.py3-none-any.whl (2.9 kB)
Requirement already satisfied: plotly>=4.1.0 in /opt/anaconda3/lib/python3.7/site-packages (from plotly_express) (4.11.0)
Requirement already satisfied: patsy>=0.5 in /opt/anaconda3/lib/python3.7/site-packages (from plotly_express) (0.5.1)
Requirement already satisfied: numpy>=1.11 in /opt/anaconda3/lib/python3.7/site-packages (from plotly_express) (1.18.1)
Requirement already satisfied: pandas>=0.20.0 in /opt/anaconda3/lib/python3.7/site-packages (from plotly_express) (1.0.1)
Requirement already satisfied: statsmodels>=0.9.0 in /opt/anaconda3/lib/python3.7/site-packages (from plotly_express) (0.11.0)
Requirement already satisfied: scipy>=0.18 in /opt/anaconda3/lib/python3.7/site-packages (from plotly_express) (1.4.1)
Requirement already satisfied: six in /opt/anaconda3/lib/python3.7/site-packages (from plotly>=4.1.0->plotly_express) (1.14.0)
Requirement already satisfied: retrying>=1.3.3 in /opt/anaconda3/lib/python3.7/site-packages (from plotly>=4.1.0->plotly_express) (1.3.3)
Requirement already satisfied: python-dateutil>=2.6.1 in /opt/anaconda3/lib/python3.7/site-packages (from pandas>=0.20.0->plotly_express) (2.8.1)
Requirement already satisfied: pytz>=2017.2 in /opt/anaconda3/lib/python3.7/site-packages (from pandas>=0.20.0->plotly_express) (2019.3)
Installing collected packages: plotly-express
Successfully installed plotly-express-0.4.1
Note: you may need to restart the kernel to use updated packages.
import plotly_express as px
import pandas as pd
import numpy as np
import plotly_express as px # 现在这种方式也可行:import plotly.express as px
# 数据集
gapminder = px.data.gapminder()
gapminder.head() # 取出前5条数据
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 0 | Afghanistan | Asia | 1952 | 28.801 | 8425333 | 779.445314 | AFG | 4 |
| 1 | Afghanistan | Asia | 1957 | 30.332 | 9240934 | 820.853030 | AFG | 4 |
| 2 | Afghanistan | Asia | 1962 | 31.997 | 10267083 | 853.100710 | AFG | 4 |
| 3 | Afghanistan | Asia | 1967 | 34.020 | 11537966 | 836.197138 | AFG | 4 |
| 4 | Afghanistan | Asia | 1972 | 36.088 | 13079460 | 739.981106 | AFG | 4 |
gapminder.shape
(1704, 8)
# line 图
fig = px.line(
gapminder, # 数据集
x="year", # 横坐标
y="lifeExp", # 纵坐标
color="continent", # 颜色的数据
line_group="continent", # 线性分组
hover_name="country", # 悬停hover的数据
line_shape="spline", # 线的形状
render_mode="svg" # 生成的图片模式
)
fig.show()
# area 图
fig = px.area(
gapminder, # 数据集
x="year", # 横坐标
y="pop", # 纵坐标
color="continent", # 颜色
line_group="country" # 线性组别
)
fig.show()
px.scatter(
gapminder # 绘图DataFrame数据集
,x="gdpPercap" # 横坐标
,y="lifeExp" # 纵坐标
,color="continent" # 区分颜色
,size="pop" # 区分圆的大小
,size_max=60 # 散点大小
)
px.scatter(
gapminder # 绘图使用的数据
,x="gdpPercap" # 横纵坐标使用的数据
,y="lifeExp" # 纵坐标数据
,color="continent" # 区分颜色的属性
,size="pop" # 区分圆的大小
,size_max=60 # 圆的最大值
,hover_name="country" # 图中可视化最上面的名字
,animation_frame="year" # 横轴滚动栏的属性year
,animation_group="country" # 标注的分组
,facet_col="continent" # 按照国家country属性进行分格显示
,log_x=True # 横坐标表取对数
,range_x=[100,100000] # 横轴取值范围
,range_y=[25,90] # 纵轴范围
,labels=dict(pop="Populations", # 属性名字的变化,更直观
gdpPercap="GDP per Capital",
lifeExp="Life Expectancy")
)
px.choropleth(
gapminder, # 数据集
locations="iso_alpha", # 配合颜色color显示
color="lifeExp", # 颜色的字段选择
hover_name="country", # 悬停字段名字
animation_frame="year", # 注释
color_continuous_scale=px.colors.sequential.Plasma, # 颜色变化
projection="natural earth" # 全球地图
)
fig = px.scatter_geo(
gapminder, # 数据
locations="iso_alpha", # 配合颜色color显示
color="continent", # 颜色
hover_name="country", # 悬停数据
size="pop", # 大小
animation_frame="year", # 数据帧的选择
projection="natural earth" # 全球地图
)
fig.show()
px.scatter_geo(gapminder, # 数据集
locations="iso_alpha", # 配和color显示颜色
color="continent", # 颜色的字段显示
hover_name="country", # 悬停数据
size="pop", # 大小
animation_frame="year" # 数据联动变化的选择
#,projection="natural earth" # 去掉projection参数
)
fig = px.line_geo(
gapminder, # 数据集
locations="iso_alpha", # 配合和color显示数据
color="continent", # 颜色
projection="orthographic") # 球形的地图
fig.show()
iris = px.data.iris()
fig = px.scatter(
iris, # 数据集
x="sepal_width", # 横坐标
y="sepal_length" # 纵坐标
)
fig.show()
px.scatter(
iris, # 数据集
x="sepal_width", # 横坐标
y="sepal_length", # 纵坐标
color="species"
)
px.scatter(
iris, # 数据集
x="sepal_width", # 横坐标
y="sepal_length", # 纵坐标
color="species", # 颜色
marginal_x="histogram", # 横坐标直方图
marginal_y="rug" # 细条图
)
px.scatter(
iris, # 数据集
x="sepal_width", # 横坐标
y="sepal_length", # 纵坐标
color="species", # 颜色
marginal_y="violin", # 纵坐标小提琴图
marginal_x="box", # 横坐标箱型图
trendline="ols" # 趋势线
)
px.scatter_matrix(
iris, # 数据
dimensions=["sepal_width","sepal_length","petal_width","petal_length"], # 维度选择
color="species") # 颜色
px.parallel_coordinates(
iris, # 数据集
color="species_id", # 颜色
labels={"species_id":"Species", # 各种标签值
"sepal_width":"Sepal Width",
"sepal_length":"Sepal Length",
"petal_length":"Petal Length",
"petal_width":"Petal Width"},
color_continuous_scale=px.colors.diverging.Tealrose,
color_continuous_midpoint=2)
# 对当前值加上下两个误差值
iris["e"] = iris["sepal_width"] / 100
px.scatter(
iris, # 绘图数据集
x="sepal_width", # 横坐标
y="sepal_length", # 纵坐标
color="species", # 颜色值
error_x="e", # 横轴误差
error_y="e" # 纵轴误差
)
px.density_contour(
iris, # 绘图数据集
x="sepal_width", # 横坐标
y="sepal_length", # 纵坐标值
color="species" # 颜色
)
px.density_contour(
iris, # 数据集
x="sepal_width", # 横坐标值
y="sepal_length", # 纵坐标值
color="species", # 颜色
marginal_x="rug", # 横轴为线条图
marginal_y="histogram" # 纵轴为直方图
)
px.density_heatmap(
iris, # 数据集
x="sepal_width", # 横坐标值
y="sepal_length", # 纵坐标值
marginal_y="rug", # 纵坐标值为线型图
marginal_x="histogram" # 直方图
)
tips = px.data.tips()
fig = px.parallel_categories(
tips, # 数据集
color="size", # 颜色
color_continuous_scale=px.colors.sequential.Inferno) # 颜色变化取值
fig.show()
px.bar(
tips, # 数据集
x="sex", # 横轴
y="total_bill", # 纵轴
color="smoker", # 颜色参数取值
barmode="group")
fig = px.bar(
tips, # 数据集
x="sex", # 横轴
y="total_bill", # 纵轴
color="smoker", # 颜色参数取值
barmode="group", # 柱状图模式取值
facet_row="time", # 行取值
facet_col="day", # 列元素取值
category_orders={
"day": ["Thur","Fri","Sat","Sun"], # 分类顺序
"time":["Lunch", "Dinner"]})
fig.show()
fig = px.histogram(
tips, # 绘图数据集
x="sex", # 横轴为性别
y="tip", # 纵轴为费用
histfunc="avg", # 直方图显示的函数
color="smoker", # 颜色
barmode="group", # 柱状图模式
facet_row="time", # 行取值
facet_col="day", # 列取值
category_orders={ # 分类顺序
"day":["Thur","Fri","Sat","Sun"],
"time":["Lunch","Dinner"]}
)
fig.show()
# notched=True显示连接处的锥形部分
px.box(tips, # 数据集
x="day", # 横轴数据
y="total_bill", # 纵轴数据
color="smoker", # 颜色
notched=True) # 连接处的锥形部分显示出来
px.box(
tips, # 数据集
x="day", # 横轴
y="total_bill", # 纵轴
color="smoker", # 颜色
# notched=True # 隐藏参数
)
px.violin(
tips, # 数据集
x="smoker", # 横轴坐标
y="tip", # 纵轴坐标
color="sex", # 颜色参数取值
box=True, # box是显示内部的箱体
points="all", # 同时显示数值点
hover_data=tips.columns) # 结果中显示全部数据
wind = px.data.wind()
fig = px.scatter_polar(
wind, # 数据集
r="frequency", # 半径
theta="direction", # 角度
color="strength", # 颜色
symbol="strength", # 线性闭合
color_discrete_sequence=px.colors.sequential.Plasma_r) # 颜色变化
fig.show()
fig = px.line_polar(
wind, # 数据集
r="frequency", # 半径
theta="direction", # 角度
color="strength", # 颜色
line_close=True, # 线性闭合
color_discrete_sequence=px.colors.sequential.Plasma_r) # 颜色变化
fig.show()
fig = px.bar_polar(
wind, # 数据集
r="frequency", # 半径
theta="direction", # 角度
color="strength", # 颜色
template="plotly_dark", # 主题
color_discrete_sequence=px.colors.sequential.Plasma_r) # 颜色变化
fig.show()
px.colors.qualitative.swatches()
px.colors.sequential.swatches()
px.scatter(
iris, # 数据集
x="sepal_width", # 横坐标值
y="sepal_length", # 纵坐标取值
color="species", # 颜色
marginal_x="box", # 横坐标为箱型图
marginal_y="histogram", # 纵坐标为直方图
height=600, # 高度
trendline="ols", # 显示趋势线
template="plotly") # 主题
px.scatter(
iris, # 数据集
x="sepal_width", # 横坐标值
y="sepal_length", # 纵坐标取值
color="species", # 颜色
marginal_x="box", # 横坐标为箱型图
marginal_y="histogram", # 纵坐标为直方图
height=600, # 高度
trendline="ols", # 显示趋势线
template="plotly_white") # 主题
px.scatter(
iris, # 数据集
x="sepal_width", # 横坐标值
y="sepal_length", # 纵坐标取值
color="species", # 颜色
marginal_x="box", # 横坐标为箱型图
marginal_y="histogram", # 纵坐标为直方图
height=600, # 高度
trendline="ols", # 显示趋势线
template="plotly_dark") # 主题
本文中利用大量的篇幅讲解了如何通过plotly_express来绘制:柱状图、线型图、散点图、小提琴图、极坐标图等各种常见的图形。通过观察上面Plotly_express绘制图形过程,我们不难发现它有三个主要的优点:
快速出图,少量的代码就能满足多数的制图要求。基本上都是几个参数的设置我们就能快速出图
图形漂亮,绘制出来的可视化图形颜色亮丽,也有很多的颜色供选择。
图形是动态可视化的。文章中图形都是截图,如果是在Jupyter notebook中都是动态图形